var path = "/OA";

$(function(){
	
	console.log("hello");
	$("#btn").click(function(){
		$.ajax({
			url :path + "/user/addUser",
			data:{
				"name":$("#name").val() , "password" : $("#password").val() ,
				"gender" : $('#gender input[type="radio"]:checked').val(), "loginname":$("#loginname").val(),
				"phonenumber":$("#phonenumber").val(),"description":$("#description").val(),
				"departmentid" :$("#departmentid").val(),"roleid" : $("#roleid").val()
				},
			dataType:"json",
			error:function(){
				alert("系统错误或者权限不足");
			},
			success:function(data){
				console.log(data);
				if(data.success){
					layer.alert(data.message,'信息提示',function(){
						location.reload();
					});
				}else{
					layer.alert(data.message);
					console.log(data);
				}
			}
		});
	});
});


function returnUser(userid){
	$.ajax({
		url:path +"/user/getUser",
		data:{"userid":userid},
		dataType:"json",
		error:function(){
			alert("系统错误");
		},
		success:function(data){
			console.log(data);
			
			if(data.success){
				$("#ename").val(data.object.name);
				$("#eloginname").val(data.object.loginname);
				$("#epassword").val(data.object.password);
				$("#ephonenumber").val(data.object.phonenumber);
				$("#edescription").val(data.object.description);
				$("#edepartmentname").val(data.object.departmentname);
				$("#erolename").val(data.object.rolename);
				$("#userid").val(data.object.id);
				$("#edit input[value='"+data.object.gender+"']").click();
				$("#edepartmentid").val(data.object.departmentid);
				$("#eroleid").val(data.object.roleid);
			}
		}
	});
}



$("#updateBtn").click(function(){
	$.ajax({
		url :path + "/user/updateUser",
		type:"post",
		data:{"name":$("#ename").val() , "password" : $("#epassword").val() ,
			"gender" : $('#edit input[type="radio"]:checked').val() , "loginname":$("#eloginname").val(),
			"phonenumber":$("#ephonenumber").val(),"description":$("#edescription").val(),
			"departmentid" :$("#edepartmentid").val(),"roleid" : $("#eroleid").val(),"id":$("#userid").val()},
		dataType:"json",
		error:function(){
			layer.alert("权限不足");
		},
		success:function(data){
			console.log(data);
			if(data.success){
				layer.alert(data.message,'信息提示',function(){
					location.reload();
				});
			}else{
				layer.alert(data.message);
				console.log(data);
			}
		}
	});
})

function sureDelete(id){
	layer.confirm("是否删除该职员?" ,{
		btn:['是','否']
	},function(){
		$.ajax({
			url : path +"/user/deleteUser",
			data:{"id" : id},
			dataType:"json",
			error:function(){
				layer.alert("权限不足");
			},
			success:function(data){
				if(data.success){
					layer.alert(data.message,'信息提示',function(){
						location.reload();
					});
				}else{
					layer.alert(data.message);
					console.log(data);
				}
			}
		});
	}
	)
}

function searchUser() {
	var search = $("#search").val();
	window.open("/OA/user/getUserList?pageId=1&search="+search ,"_self");
}
